package com.exam.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DelayQueue {
    public static final String DELAY_QUEUE_NAME = "delay_send_queue";
    public static final String DELAY_EXCHANGE_NAME = "delay_send_exchange";
    public static final String DEAD_LETTER_QUEUE_NAME = "dead_letter_queue";
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead_letter_exchange";
    public static final String DELAY_ROUTING_KEY = "delay_routing_key";
    public static final String DEAD_LETTER_ROUTING_KEY = "dead_letter_routing_key";
    @Bean(DELAY_QUEUE_NAME)
    public Queue delayQueue() {
        return QueueBuilder.durable(DELAY_QUEUE_NAME)
                .ttl(25*1000)
                .deadLetterExchange(DEAD_LETTER_EXCHANGE_NAME)
                .deadLetterRoutingKey(DEAD_LETTER_ROUTING_KEY)
                .build();
    }

    @Bean(DELAY_EXCHANGE_NAME)
    public DirectExchange delayExchange() {
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    @Bean(DEAD_LETTER_QUEUE_NAME)
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }

    @Bean(DEAD_LETTER_EXCHANGE_NAME)
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }
    @Bean
    public Binding delaySendBinding(@Qualifier(DELAY_QUEUE_NAME) Queue delayQueue,@Qualifier(DELAY_EXCHANGE_NAME) DirectExchange delayExchange) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(DELAY_ROUTING_KEY);
    }
    @Bean
    public Binding deadLetterBinding(@Qualifier(DEAD_LETTER_QUEUE_NAME) Queue deadLetterQueue,@Qualifier(DEAD_LETTER_EXCHANGE_NAME) DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with(DEAD_LETTER_ROUTING_KEY);
    }
}
